package com.woniu.microworkorderserver.businessmag.controller;

import com.github.pagehelper.PageInfo;
import com.google.inject.internal.cglib.proxy.$FixedValue;
import com.woniu.microcommonsserver.entity.PollingTemplateInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microcommonsserver.param.PageResult;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microworkorderserver.businessmag.service.InspectTemplateService;
import com.woniu.microworkorderserver.util.UploadImg;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/inspectTemplate")
public class InspectTemplateController {

    @Resource
    private InspectTemplateService inspectTemplateServiceImpl;

    @PutMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg save(PollingTemplateInfoEntity templateInfoEntity){
        inspectTemplateServiceImpl.save(templateInfoEntity);
        return ResponseMsg.builder().code(200).msg("新增成功").build();
    }

    @GetMapping(value = "/page",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByPage(PageRequest request,PollingTemplateInfoEntity entity){
        PageInfo<PollingTemplateInfoEntity> page = inspectTemplateServiceImpl.findByPage(request, entity);
        PageResult<PollingTemplateInfoEntity> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getList());
        return ResponseMsg.builder().code(200).msg("查询成功").data(pageResult).build();
    }

    @PostMapping(value = "/upload",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg uploadImg(@RequestParam("file") MultipartFile file){
        System.out.println(file.getOriginalFilename());
        UploadImg.getUploadImgUrl(file);
        return ResponseMsg.builder().code(200).msg("上传成功").build();
    }

    @GetMapping(value = "/findAllName",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findAllName(){
        List<PollingTemplateInfoEntity> list = inspectTemplateServiceImpl.findAllName();
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }
}
